package api.cie.rss.dal;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import api.cie.rss.common.RSS;

/**
 * The class to manipulate with the RSS table in the database
 * @author STL-API
 *
 */
public class RssDAL {

	public static final String KEY_TITLE = "title";
	public static final String KEY_URL = "url";
	public static final String KEY_ISACTIVE = "isActive";
	public static final String KEY_ROWID = "_id";
	
	private static final String DATABASE_TABLE = "RSS";
	private SQLiteDatabase database;
	
	public RssDAL(DatabaseAdapter dbAdapter) {
		database = dbAdapter.getDatabase();
	}
	
	/**
	 * Create a new Rss. If the Rss is successfully created, return the new
	 * rowId for that Rss, otherwise return a -1 to indicate failure.
	 */
	public long createRSS(String title, String url, int isActive) {
		ContentValues initialValues = createContentValues(title, url,
				isActive);

		return database.insert(DATABASE_TABLE, null, initialValues);
	}
	
	private ContentValues createContentValues(String title, String url,
			int isActive) {
		ContentValues values = new ContentValues();
		values.put(KEY_TITLE, title);
		values.put(KEY_URL, url);
		values.put(KEY_ISACTIVE, isActive);
		
		return values;
	}
	
	/**
	 * Deletes Rss
	 */
	public boolean deleteRSS(long rowId) {
		return database.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
	}

	/**
	 * Return a Cursor over the list of all Rss in the database
	 * 
	 * @return Cursor over all Rss
	 */
	public Cursor fetchAllRSS() {
		return database.query(DATABASE_TABLE, new String[] { KEY_ROWID,
				KEY_TITLE, KEY_URL, KEY_ISACTIVE }, null, null, null,
				null, null);
	}

	/**
	 * Return a Cursor positioned at the defined Rss
	 */
	public Cursor fetchRSS(long rowId) throws SQLException {
		Cursor mCursor = database.query(true, DATABASE_TABLE, new String[] {
				KEY_ROWID, KEY_TITLE, KEY_URL, KEY_ISACTIVE },
				KEY_ROWID + "=" + rowId, null, null, null, null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}
	
	/**
	 * Return the list of all Rss in the database
	 * @return list of all Rss
	 */
	public List<RSS> getListRSS(){
		Cursor cursorRSS = fetchAllRSS();
		List<RSS> listRSS = new ArrayList<RSS>();
		for(int i=0; i<cursorRSS.getCount(); i++) {
            cursorRSS.moveToPosition(i);
            RSS rss = new RSS();
            rss.setId(cursorRSS.getInt(cursorRSS.getColumnIndex(KEY_ROWID)));
            rss.setActive((cursorRSS.getInt(cursorRSS.getColumnIndex(KEY_ISACTIVE)))==0?false:true);
            rss.setTitle(cursorRSS.getString(cursorRSS.getColumnIndex(RssDAL.KEY_TITLE)));
            rss.setUrl(cursorRSS.getString(cursorRSS.getColumnIndex(RssDAL.KEY_URL)));
            listRSS.add(rss);
		}
		
		return listRSS;		
	}
	
	/**
	 * Update a attribut isActive in the database
	 * @param rowId
	 * @param newValue
	 */
	public void updateIsActive(long rowId, int newValue) {
		ContentValues values = new ContentValues();
		values.put(KEY_ISACTIVE, newValue);
		database.update(DATABASE_TABLE, values, KEY_ROWID+" = " + rowId , null);
	}
	
}
